//
//  DCAuthBankCardController.m
//  Demo
//
//  Created by zhaolin on 2023/1/9.
//

#import "DCAuthBankCardController.h"
#import "DCAuthInfoUIModel.h"
#import "DCAuthFooterView.h"
#import "DCAuthTipsCell.h"
#import "DCBankModel.h"
#import "DCPersonalInfoBankPicker.h"
@interface DCAuthBankCardController ()<UITableViewDelegate,UITableViewDataSource,DCAuthInfoCommonCellDelegate,DCAuthFooterViewDelegate>

@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) DCPersonalInfoBankPicker *pickView;
@property (nonatomic, strong) NSIndexPath *currentIndex;
@property (nonatomic, strong) NSArray *bankArr;
@end

@implementation DCAuthBankCardController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitleName:@"Account Information"];
    [self getDataFromNet];
    // Do any additional setup after loading the view.
}

- (void)configureAlloc {
    [super configureAlloc];
    self.step = @"BANK_INFO";
    self.dataSource = @[].mutableCopy;
    NSArray *titleArr = @[@"Bank",@"Bank Account",@"Confirm Bank Account"];
    NSArray *placeholderArr = @[@"Select",@"Enter your bank account",@"Confirm your bank account"];
    NSArray *cellTypeArr = @[@(DCAuthInfoCommonCellTypeRightRight),@(DCAuthInfoCommonCellTypeRightNun),@(DCAuthInfoCommonCellTypeRightNun)];
    NSArray *canEdit = @[@(NO),@(YES),@(YES)];
    for (int i = 0; i<titleArr.count; i++) {
        DCAuthInfoUIModel *model = [DCAuthInfoUIModel new];
        model.title = [titleArr objectAtIndex:i];
        model.placeholder = [placeholderArr objectAtIndex:i];
        model.celltype = [[cellTypeArr objectAtIndex:i] intValue];
        model.canEdit = [[canEdit objectAtIndex:i] boolValue];
        [self.dataSource addObject:model];
    }
}
- (void) getDataFromNet {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [DCHttpRequest DCBankListCompleteBlock:^(NSURLSessionDataTask * _Nonnull operation, DCResponseData * _Nonnull responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (responseObject.code == 200) {
            NSArray *dataArr = [DCBankModel mj_objectArrayWithKeyValuesArray:[responseObject.data objectForKey:@"banks"]];
            self.bankArr = dataArr;
            [self.pickView updateDataArr:dataArr];
        } else {
            [DCDCToast toastText:responseObject.message];
        }
    }];
}
- (void)configureUI {
    [super configureUI];
    [super _setCurrntStep:5];
    [self configureTable];
    [self.tableView registerClass:[DCAuthInfoCommonCell class] forCellReuseIdentifier:NSStringFromClass(DCAuthInfoCommonCell.class)];
    [self.tableView registerClass:[DCAuthTipsCell class] forCellReuseIdentifier:NSStringFromClass(DCAuthTipsCell.class)];
    [self.tableView registerClass:[DCAuthFooterView class] forHeaderFooterViewReuseIdentifier:NSStringFromClass(DCAuthFooterView.class)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.topRadiusView.mas_bottom).mas_equalTo(-30);
    }];
    [self.view bringSubviewToFront:self.tableView];
    
    mWeakSelf;
    _pickView = [[DCPersonalInfoBankPicker alloc] initWithFrame:CGRectZero];
    _pickView.cancelCallBack = ^(DCBankModel * _Nonnull date) {
        [weakSelf pickViewShow:NO];
    };
    _pickView.sureCallBack = ^(DCBankModel * _Nonnull date) {
        DCAuthInfoUIModel *model = weakSelf.dataSource[weakSelf.currentIndex.row];
        model.content = date.bankName;
        model.bankModel = date;
        [weakSelf pickViewShow:NO];
        [weakSelf.tableView reloadData];
    };
    [self.view addSubview:_pickView];
    [_pickView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view.height);
        make.bottom.mas_equalTo(-self.view.height);
    }];
}
#pragma mark - tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count +1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row ==3) {
        DCAuthTipsCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(DCAuthTipsCell.class)];
        [cell setTipString:@"1.Add a bank account to receive your loan\n2.Please make sure to use your own bank account\n3.Your account details are safely protected,please feel free to fill in\n4.Bank account switch is not allowed when applying for loans"];
        return cell;
    } else {
        DCAuthInfoUIModel *model = [self.dataSource objectAtIndex:indexPath.row];
        DCAuthInfoCommonCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(DCAuthInfoCommonCell.class)];
        cell.delegate = self;
        [cell configureDataWithModel:model];
        cell.indexPath = indexPath;
        return cell;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    //    DCAuthInfoUIModel *model = [self.dataSource objectAtIndex:indexPath.row];
    if (indexPath.row ==3) {
        return UITableViewAutomaticDimension;
    }else {
        return 105;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    DCAuthFooterView *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass(DCAuthFooterView.class)];
    BOOL contentNull = [self allInfoContentNull];
    [footer setCommitBtnEnabled:!contentNull];
    footer.delegate = self;
    return footer;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 125;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}
- (BOOL)allInfoContentNull {
    BOOL contentNull= NO;
    DCAuthInfoUIModel *model0 = [self.dataSource objectAtIndex:0];
    DCAuthInfoUIModel *model1 = [self.dataSource objectAtIndex:1];
    DCAuthInfoUIModel *model2 = [self.dataSource objectAtIndex:2];
    if(!model0.bankModel) {
        contentNull = YES;
    } else if (!model1.content  || !model1.content ||
               !model2.content  || !model2.content ||
               ![model1.content isEqualToString:model2.content]) {
        contentNull = YES;
    }
    
    return contentNull;
}
#pragma mark -- DCAuthInfoCommonCellDelegate
- (void)authInfoCommonCellBegainEditingWithText:(NSIndexPath *)index {
    _currentIndex = index;
    if(_currentIndex.row != 0) {
        return;
    }
    if(self.bankArr && self.bankArr.count) {
        [self pickViewShow: YES];
    }else {
        [self getDataFromNet];
    }
        //    [self getDictListWithType:type];
}
- (void)authInfoCommonCellEndEditingWithText:(NSString *)text {
    [self.tableView reloadData];
}
#pragma mark --- DCAuthFooterViewDelegate
- (void)authFooterViewNextAction:(id _Nullable )sender {
    /*
     data: "{
       \"bankCode\":\"2001\", // 用户选中的银行的银行编号
       \"bankName\":\"BANXICO\", // 用户选中的银行的银行名称
       \"bankCard\":\"123456789012345678\",// 银行卡号
       }" */
    
    BOOL contentNull = [self allInfoContentNull];
    if(contentNull) {
        return;
    }
    
    NSMutableDictionary *mDic = @{}.mutableCopy;
    DCAuthInfoUIModel *model0 = [self.dataSource objectAtIndex:0];
    [mDic setValue:model0.bankModel.bankCode forKey:@"bankCode"];
    [mDic setValue:model0.bankModel.bankName forKey:@"bankName"];

    DCAuthInfoUIModel *model1 = [self.dataSource objectAtIndex:1];
    [mDic setValue:model1.content forKey:@"bankCard"];
    
    NSString *jsonStr = mDic.mj_JSONString;
    [self saveInfo2:jsonStr];

}

- (void)authFooterViewAgreementAction:(id _Nullable )sender {
    [self pushPrivacyAgreement];
}
- (void)pickViewShow:(BOOL)show {
    [self.pickView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(show?0:self.view.height);
        make.bottom.mas_equalTo(show?0:-self.view.height);
    }];
    self.pickView.backgroundColor = [[UIColor colorWithString:@"#000000"] colorWithAlphaComponent:0];

    [UIView animateWithDuration:0.5 animations:^{
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        self.pickView.backgroundColor = [[UIColor colorWithString:@"#000000"] colorWithAlphaComponent:0.7];
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
